//
// Templet.h
//
// $Id: //base/include/Templet.h
//
// Library: base
// Package: base
// Module : Templet
// Author :	Jelin,doitsjz@163.com
// Date	  :	10/30/2012
// 
// Definition of the Templet class.
//
// Copyright (c) 2012-2012, iGuard.
//

#include "RTSPClientSession.h"
#include <sstream>
#include <map>

RTSPClientSession::RTSPClientSession(string &strURL):m_strURL(strURL)
{
}
RTSPClientSession::RTSPClientSession()
{
}
RTSPClientSession::~RTSPClientSession()
{

}

int RTSPClientSession::handleMessage(RTSPRequest& request)
{
	return 0;
}
int RTSPClientSession::handleMessage(RTSPResponse& reply)
{
	string method;
	int seqid = reply.getCSeq();	
	bool bRet = findCSeq(seqid,method);
	if (method == RTSPRequest::DESCRIBE)
		OnDescribeReply(reply.getStatus(),reply);
	else if (method == RTSPRequest::SETUP)
		OnSetupReply(reply.getStatus(),reply);
	else if (method == RTSPRequest::PLAY)
		OnPlayReply(reply.getStatus(),reply);
	else if (method == RTSPRequest::PAUSE)
		OnPauseReply(reply.getStatus(),reply);
	else if (method == RTSPRequest::RECORD)
		OnRecordReply(reply.getStatus(),reply);
	else if (method == RTSPRequest::TEARDOWN)
		OnTeardownReply(reply.getStatus(),reply);
	else if (method == RTSPRequest::OPTIONS)
		OnOptionsReply(reply.getStatus(),reply);
	else
		;//default nothing ,future add err deal or selfdefine method
	return true;
}
void RTSPClientSession::OnOptionsReply(int status,RTSPResponse& reply)
{
	cout << "RTSPClientSession::OnOptopmsReply status="<<status <<endl;
}
void RTSPClientSession::OnDescribeReply(int status,RTSPResponse& reply)
{
	cout << "RTSPClientSession::OnDescribeReply status ="<<status <<endl;
}


void RTSPClientSession::OnSetupReply(int status,RTSPResponse& reply)
{
	cout << "RTSPClientSession::OnSetupReply status="<<status <<endl;
}
void RTSPClientSession::OnPlayReply(int status,RTSPResponse& reply)
{
	cout << "RTSPClientSession::OnPlayReply status="<<status <<endl;
}
void RTSPClientSession::OnPauseReply(int status,RTSPResponse& reply)
{
	cout << "RTSPClientSession::OnPauseReply status="<<status <<endl;
}
void RTSPClientSession::OnRecordReply(int status,RTSPResponse& reply)
{
	cout << "RTSPClientSession::OnRecordReply status="<<status <<endl;
}
void RTSPClientSession::OnTeardownReply(int status,RTSPResponse& reply)
{
	cout << "RTSPClientSession::OnTeardownReply status="<<status <<endl;
}

int RTSPClientSession::makeDescribeRequest(string& req)
{
	RTSPRequest request(RTSPRequest::DESCRIBE, m_strURL);
	
	int seqid = getCSeqID();
	request.setCSeq(seqid);
	request.setDate();
	request.setUserAgent();
	request.setAccept();
	
	std::ostringstream ostr;	
	request.write(ostr);
	req = ostr.str();

	addCSeq(seqid,RTSPRequest::DESCRIBE);

	return 0;
}
int RTSPClientSession::makeSetupRequest(int port,string& req)
{
	CRtspTransport tp;
	tp.setClientPort(port);

	RTSPRequest request(RTSPRequest::SETUP, m_strURL);
	
	int seqid = getCSeqID();
	request.setCSeq(seqid);
	request.setDate();
	request.setUserAgent();
	request.setAccept();
	request.setTransport(tp);

	std::ostringstream ostr;
	request.write(ostr);
	req = ostr.str();

	addCSeq(seqid,RTSPRequest::SETUP);

	return 0;
}
int RTSPClientSession::makePlayRequest(float fStart,string& req)
{
	CRange range;
	range.seRange(fStart);

	RTSPRequest request(RTSPRequest::PLAY, m_strURL);
	
	int seqid = getCSeqID();
	request.setCSeq(seqid);
	request.setDate();
	request.setUserAgent();
	request.setAccept();
	request.setRange(range);
	//for test code
//	request.setSession(generateUUID());
	request.setSession(getSessionID());
	std::ostringstream ostr;
	request.write(ostr);
	req = ostr.str();

	addCSeq(seqid,RTSPRequest::PLAY);

	return 0;
}
int RTSPClientSession::makePauseRequest(string& req)
{
	RTSPRequest request(RTSPRequest::PAUSE, m_strURL);
	
	int seqid = getCSeqID();
	request.setCSeq(seqid);
	request.setDate();
	request.setUserAgent();
	request.setSession(getSessionID());
//	request.setSession(generateUUID());
	std::ostringstream ostr;
	request.write(ostr);
	req = ostr.str();

	addCSeq(seqid,RTSPRequest::PAUSE);

	return 0;
}
int RTSPClientSession::makeTeardownRequest(string& req)
{
	RTSPRequest request(RTSPRequest::TEARDOWN, m_strURL);
	
	int seqid = getCSeqID();
	request.setCSeq(seqid);
	request.setDate();
	request.setUserAgent();
	request.setSession(getSessionID());
//	request.setSession(generateUUID());
	std::ostringstream ostr;
	request.write(ostr);
	req = ostr.str();

	addCSeq(seqid,RTSPRequest::TEARDOWN);

	return 0;
}
